package com.zeyuan.rpcclient.netty.future;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

public class NettyFuture {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //netty中线程池是eventgroup，且只有一个线程
        NioEventLoopGroup group = new NioEventLoopGroup();
        EventLoop eventLoop = group.next();
        Future<Integer> future = eventLoop.submit(new Callable<Integer>() {

            @Override
            public Integer call() throws Exception {
                return null;
            }
        });
        //netty中的future
        //同步方式获取结果
        //Integer integer = future.get();
        //异步方式获取结果,GenericFutureListener,不能用channelfuturelistener，因为nioeventloop不属于channel
        //之前用channelfuturelistener是因为future是channel.submit获取到的。现在是eventloop.submit
        future.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                //获取结果，同步即可，因为回调的时候肯定获取到了结果
                Object now = future.getNow();


            }
        });


    }
}
